package GUI.UI
{
    import GUI.UI.Events.UIEvent;
    import GUI.UI.Events.UIScrollEvent;
    
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.geom.Transform;
    
    public class UIEntity
    {
        protected var _baseSprite:Sprite;
        protected var _listeningUIEventsFrom:Vector.<UIEntity>;
        
        public function UIEntity(baseSprite:Sprite)
        {
            _baseSprite            = baseSprite;
            _listeningUIEventsFrom = new Vector.<UIEntity>();
        }
        
        /**
         * Every GuiEntity destroys his childs, and removes himself from parent.
         */
        public function destroy():void
        {
            var child:UIEntity;
            
            for(var x:int = 0; x < this.numChildren; x++)
            {
                child = this.getChildAt(x) as UIEntity;
                
                child.destroy();
            }
            
            if(_baseSprite && _baseSprite.parent)
                _baseSprite.parent.removeChild(_baseSprite);
            
            unListenAllUIEvents();
            
            _baseSprite = null;
        }
               
        public function get baseSprite():Sprite
        {
            return _baseSprite;
        }
        
        public function processUIEvent(e:UIEvent):void
        {
            
        }
        
        public function ListenUIEventsFrom(object:UIEntity):void
        {
            object.addEventListener(UIScrollEvent.SCROLL_MORE, processUIEvent);
            object.addEventListener(UIScrollEvent.SCROLL_LESS, processUIEvent);
            
            _listeningUIEventsFrom.push(object);
        }
        
        public function unListenAllUIEvents():void
        {
            for each (var object:UIEntity in _listeningUIEventsFrom)
            {
                if(object.hasEventListener(UIScrollEvent.SCROLL_MORE))
                    object.removeEventListener(UIScrollEvent.SCROLL_MORE, processUIEvent);
                    
                if(object.hasEventListener(UIScrollEvent.SCROLL_LESS))
                    object.removeEventListener(UIScrollEvent.SCROLL_LESS, processUIEvent);
            }
            
            _listeningUIEventsFrom.length = 0;
        }
        
        //Redirect methods to _baseSprite
        public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = true):void
        {
            if(this._baseSprite)
                _baseSprite.addEventListener(type, listener, useCapture, priority, useWeakReference);
        }
        
        public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
        {
            if(this._baseSprite)
                _baseSprite.removeEventListener(type, listener, useCapture);
        }
        
        public function hasEventListener(type:String):Boolean
        {
            if(this._baseSprite)
                return _baseSprite.hasEventListener(type);
            
            return false;
        }
        
        public function dispatchEvent(e:Event):Boolean
        {
            if(this._baseSprite)
                return _baseSprite.dispatchEvent(e);
            
            return false;
        }
        
        public function get numChildren():int
        {
            if(this._baseSprite)
                return _baseSprite.numChildren;
            
            return -1;
        }
        
        public function getChildAt(index:int):DisplayObject
        {
            if(this._baseSprite)
                return _baseSprite.getChildAt(index);
            
            return null;
        }
        
        public function get parent():DisplayObjectContainer
        {
            if(this._baseSprite)
                return _baseSprite.parent;
            
            return null;
        }
        
        public function get scrollRect():Rectangle
        {
            if(this._baseSprite)
                return _baseSprite.scrollRect;
            
            return null;
        }

        public function set scrollRect(rect:Rectangle):void
        {
            if(this._baseSprite)
                _baseSprite.scrollRect = rect;
        }
        
        public function get stage():Stage
        {
            if(this._baseSprite)
                return _baseSprite.stage;
            
            return null;
        }
        
        public function get width():Number
        {
            if(this._baseSprite)
                return _baseSprite.width;
            
            return -1;
        }
        
        public function set width(value:Number):void
        {
            if(this._baseSprite)
                _baseSprite.width = value;
        }

        public function get x():Number
        {
            if(this._baseSprite)
                return _baseSprite.x;
            
            return 0;
        }
        
        public function set x(value:Number):void
        {
            if(this._baseSprite)
                _baseSprite.x = value;
        }

        public function get y():Number
        {
            if(this._baseSprite)
                return _baseSprite.y;
            
            return 0;
        }
        
        public function set y(value:Number):void
        {
            
            if(this._baseSprite)
                _baseSprite.y = value;
        }
        
        public function get height():Number
        {
            if(this._baseSprite)
                return _baseSprite.height;
            
            return 0;
        }
        
        public function set height(value:Number):void
        {
            if(this._baseSprite)
                _baseSprite.height = value;
        }
        
        public function get scaleX():Number
        {
            if(this._baseSprite)
                return _baseSprite.scaleX;
            
            return 0;
        }

        public function set scaleX(value:Number):void
        {
            if(this._baseSprite)
                _baseSprite.scaleX = value;
        }
        
        public function get scaleY():Number
        {
            if(this._baseSprite)
                return _baseSprite.scaleY;
            
            return 0;
        }
        
        public function set scaleY(value:Number):void
        {
            if(this._baseSprite)
                _baseSprite.scaleY = value;
        }
        
        public function get transform():Transform
        {
            if(this._baseSprite)
                return _baseSprite.transform;
            
            return null;
        }
        
        public function addChild(child:UIEntity):void
        {
            if(this._baseSprite)
                _baseSprite.addChild(child.baseSprite);
        }
        
        public function addChildAt(child:UIEntity, index:int):void
        {
            if(this._baseSprite)
                _baseSprite.addChildAt(child.baseSprite, index);
        }
        
        public function removeChild(child:DisplayObject):void
        {
            if(this._baseSprite)
                _baseSprite.removeChild(child)
        }
        
        public function get graphics():Graphics
        {
            if(this._baseSprite)
                return _baseSprite.graphics;
            
            return null;
        }
        //
    }
}